

package com.hazelcast.replicatedmap.impl.operation;

import com.hazelcast.internal.util.UUIDSerializationUtil;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.replicatedmap.impl.ReplicatedMapService;
import com.hazelcast.replicatedmap.impl.iterator.ReplicatedMapIterationService;
import com.hazelcast.spi.impl.operationservice.ReadonlyOperation;

import java.io.IOException;
import java.util.UUID;

public class EndEntryViewIterationOperation extends AbstractNamedSerializableOperation implements ReadonlyOperation {
    private UUID cursorId;
    private String name;

    public EndEntryViewIterationOperation() {
    }

    public EndEntryViewIterationOperation(String name, UUID cursorId) {
        this.name = name;
        this.cursorId = cursorId;
    }

    @Override
    public int getClassId() {
        return ReplicatedMapDataSerializerHook.END_ENTRYVIEW_ITERATION;
    }

    @Override
    public void run() throws Exception {
        ReplicatedMapService service = getService();
        ReplicatedMapIterationService iterationService = service.getIterationService();
        iterationService.getIteratorManager().cleanupIterator(cursorId);
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getServiceName() {
        return ReplicatedMapService.SERVICE_NAME;
    }

    @Override
    protected void writeInternal(ObjectDataOutput out) throws IOException {
        out.writeString(name);
        UUIDSerializationUtil.writeUUID(out, cursorId);
    }

    @Override
    protected void readInternal(ObjectDataInput in) throws IOException {
        name = in.readString();
        cursorId = UUIDSerializationUtil.readUUID(in);
    }
}
